package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.YddUserDto;
import com.itheima.entity.YddUser;
import com.itheima.mapper.YddUserMapper;
import com.itheima.service.YddUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.entity.YddUser;
import com.itheima.mapper.YddUserMapper;
import com.itheima.service.YddUserService;
import org.springframework.stereotype.Service;

@Service
public class YddUserServiceImpl extends ServiceImpl<YddUserMapper, YddUser> implements YddUserService {

    @Autowired
    private YddUserService yddUserService;

    /**
     * 分页查询
     *
     * @param sex
     * @param username
     * @param mobile
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public YddUserDto getList(Integer sex, String username, String mobile, Integer page, Integer pageSize) {

        //构造查询
        Page<YddUser> yddUserPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<YddUser> yddUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        yddUserLambdaQueryWrapper.eq(sex != null, YddUser::getSex, sex);
        yddUserLambdaQueryWrapper.like(StringUtils.isNotEmpty(username), YddUser::getUsername, username);
        yddUserLambdaQueryWrapper.like(StringUtils.isNotEmpty(mobile), YddUser::getMobile, mobile);
        yddUserLambdaQueryWrapper.orderByDesc(YddUser::getId);
        //查询
        Page<YddUser> pageInfo = yddUserService.page(yddUserPage, yddUserLambdaQueryWrapper);
        //数据封装
        YddUserDto yddUserDto = new YddUserDto();
        yddUserDto.setCount((int) pageInfo.getTotal());
        yddUserDto.setCurrentPage((int) pageInfo.getCurrent());
        yddUserDto.setData(pageInfo.getRecords());
        yddUserDto.setPageSize((int) pageInfo.getSize());
        yddUserDto.setTotalPages((int) pageInfo.getPages());

        return yddUserDto;
    }
}
